package com.starsoft.smdc.dao;

import java.util.Date;

import org.springframework.stereotype.Repository;

import com.starsoft.frame.core.orm.BaseDao;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.smdc.bean.SmdcDiv;
import com.starsoft.smdc.bean.SmdcDivId;
import com.starsoft.smdc.bean.SmdcEr;
import com.starsoft.smdc.bean.SmdcErId;
import com.starsoft.smdc.bean.SmdcFundamental;

@Repository
public class FundamentalDao extends BaseDao<SmdcFundamental, String>{
	
	public SmdcFundamental get(String secId) {
		SmdcFundamental fundmatental = super.get(secId);
		if (fundmatental == null) {
			fundmatental = new SmdcFundamental(secId);
			save(fundmatental);
			this.getSession().flush();
			this.evict(fundmatental);
			fundmatental = super.get(secId);
		}
		return fundmatental;
	}
	
	public void deleteBySecId(String secId){
		SmdcFundamental fundmatental = super.get(secId);
		if (fundmatental != null) {
			for(SmdcDiv div : fundmatental.getSmdcDivs()){
				this.delete(div);
			}
			for(SmdcEr er : fundmatental.getSmdcErs()){
				this.delete(er);
			}
			this.delete(fundmatental);
		}
	}
	
	
	public void evictFundamental(String secId){
		SmdcFundamental fundmatental = get(secId);
		evict(fundmatental);
		getSessionFactory().getCache().evictEntity(SmdcFundamental.class, secId);
	}
	
	public SmdcEr getEr(SmdcErId id){
 		SmdcFundamental fundamental = get(id.getSecId());
		Date reportDate = DateUtil.floorTime(id.getReportDate());
		SmdcEr result = null;
		for(SmdcEr er : fundamental.getSmdcErs()){
			Date reportDateT = er.getId().getReportDate();
			if(DateUtil.isSameDay(reportDateT, reportDate)){
				result = er;
				break;
			}
		}
		return result;
	}
	
	public void deleteEr(SmdcEr er){
		this.delete(er);
		String secId = er.getId().getSecId();
		SmdcFundamental fundmatental = get(secId);
		SmdcEr cachEr = getEr(er.getId());
		if( cachEr!=null ){
			fundmatental.getSmdcErs().remove(cachEr);
		}
	}
	
	public void saveEr(SmdcEr er){
		saveOrUpdate(er);
		String secId = er.getId().getSecId();
		SmdcFundamental fundmatental = get(secId);
		if( getEr(er.getId())==null ){
			fundmatental.getSmdcErs().add(er);
		}
	}

	public SmdcDiv getDiv(SmdcDivId id){
		SmdcFundamental fundamental = get(id.getSecId());
		Date divDate = DateUtil.floorTime(id.getDivDate());
		SmdcDiv result = null;
		for(SmdcDiv div : fundamental.getSmdcDivs()){
			Date divDatet = div.getId().getDivDate();
			if(divDatet.equals(divDate)){
				result = div;
				break;
			}
		}
		return result;
	}
	
	public void deleteDiv(SmdcDiv div){
		this.delete(div);
		String secId = div.getId().getSecId();
		SmdcFundamental fundmatental = get(secId);
		SmdcDiv cachDiv = getDiv(div.getId());
		if( cachDiv!=null ){
			fundmatental.getSmdcDivs().remove(cachDiv);
		}
	}
	
	public void saveDiv(SmdcDiv div){
		saveOrUpdate(div);
		String secId = div.getId().getSecId();
		SmdcFundamental fundmatental = get(secId);
		if( getDiv(div.getId())==null ){
			fundmatental.getSmdcDivs().add(div);
		}
	}
}
